// UserProgressMapper.java
package com.example.gymlearning.mapper;

import com.example.gymlearning.entity.UserPointProgress;
import com.example.gymlearning.entity.UserPackageProgress;
import org.apache.ibatis.annotations.*;

@Mapper
public interface UserProgressMapper {

    // 知识点进度相关
    @Select("SELECT * FROM user_point_progress WHERE user_id = #{userId} AND point_id = #{pointId}")
    UserPointProgress findPointProgress(@Param("userId") Long userId, @Param("pointId") Long pointId);

    @Insert("INSERT INTO user_point_progress (user_id, point_id, status, best_score, start_time, complete_time, attempt_count, learning_count, total_duration_seconds, created_time, updated_time) " +
            "VALUES (#{userId}, #{pointId}, #{status}, #{bestScore}, #{startTime}, #{completeTime}, #{attemptCount}, #{learningCount}, #{totalDurationSeconds}, #{createdTime}, #{updatedTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertPointProgress(UserPointProgress progress);

    @Update("UPDATE user_point_progress SET status = #{status}, best_score = #{bestScore}, start_time = #{startTime}, " +
            "complete_time = #{completeTime}, attempt_count = #{attemptCount}, learning_count = #{learningCount}, " +
            "total_duration_seconds = #{totalDurationSeconds}, updated_time = #{updatedTime} " +
            "WHERE id = #{id}")
    void updatePointProgress(UserPointProgress progress);

    // 知识包进度相关
    @Select("SELECT * FROM user_package_progress WHERE user_id = #{userId} AND package_id = #{packageId}")
    UserPackageProgress findPackageProgress(@Param("userId") Long userId, @Param("packageId") Long packageId);

    @Insert("INSERT INTO user_package_progress (user_id, package_id, status, gateway_unlocked, gateway_completed, " +
            "gateway_score, gateway_start_time, gateway_complete_time, completed_points, total_points, created_time, updated_time) " +
            "VALUES (#{userId}, #{packageId}, #{status}, #{gatewayUnlocked}, #{gatewayCompleted}, #{gatewayScore}, " +
            "#{gatewayStartTime}, #{gatewayCompleteTime}, #{completedPoints}, #{totalPoints}, #{createdTime}, #{updatedTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertPackageProgress(UserPackageProgress progress);

    @Update("UPDATE user_package_progress SET status = #{status}, gateway_unlocked = #{gatewayUnlocked}, " +
            "gateway_completed = #{gatewayCompleted}, gateway_score = #{gatewayScore}, gateway_start_time = #{gatewayStartTime}, " +
            "gateway_complete_time = #{gatewayCompleteTime}, completed_points = #{completedPoints}, updated_time = #{updatedTime} " +
            "WHERE id = #{id}")
    void updatePackageProgress(UserPackageProgress progress);

    // 统计已完成的知识点数量
    @Select("SELECT COUNT(*) FROM user_point_progress upp " +
            "JOIN knowledge_point kp ON upp.point_id = kp.id " +
            "WHERE upp.user_id = #{userId} AND kp.package_id = #{packageId} AND upp.status = 'COMPLETED'")
    int getCompletedPointsCount(@Param("userId") Long userId, @Param("packageId") Long packageId);

    // 学习时长统计
    @Select("SELECT COALESCE(total_duration_seconds, 0) FROM user_point_progress " +
            "WHERE user_id = #{userId} AND point_id = #{pointId}")
    Integer getPointDurationSeconds(@Param("userId") Long userId, @Param("pointId") Long pointId);

    @Select("SELECT COALESCE(SUM(upp.total_duration_seconds), 0) " +
            "FROM user_point_progress upp " +
            "JOIN knowledge_point kp ON upp.point_id = kp.id " +
            "WHERE upp.user_id = #{userId} AND kp.package_id = #{packageId}")
    Integer getPackageDurationSeconds(@Param("userId") Long userId, @Param("packageId") Long packageId);

    @Select("SELECT COALESCE(SUM(upp.total_duration_seconds), 0) " +
            "FROM user_point_progress upp " +
            "JOIN knowledge_point kp ON upp.point_id = kp.id " +
            "WHERE upp.user_id = #{userId} AND kp.module_id = #{moduleId}")
    Integer getModuleDurationSeconds(@Param("userId") Long userId, @Param("moduleId") Long moduleId);

    @Select("SELECT COALESCE(SUM(upp.total_duration_seconds), 0) " +
            "FROM user_point_progress upp " +
            "JOIN knowledge_point kp ON upp.point_id = kp.id " +
            "WHERE upp.user_id = #{userId} AND kp.class_id = #{classId}")
    Integer getClassDurationSeconds(@Param("userId") Long userId, @Param("classId") Long classId);
}